<template>
    <div class="manage">
        <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="50%" :before-close="handleClose">
        <!-- 用户表单信息 -->
        <el-form ref="form" :rules="rules" :inline="true" :model="form" label-width="90px">
          <el-form-item label="用户名" prop="username">
            <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input placeholder="请输入密码" v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input placeholder="请输入邮箱" v-model="form.email"></el-input>
          </el-form-item>
          <el-form-item label="权限" prop="roleid" v-model="form.roleid">    
            <el-select v-model="form.roleid" placeholder="请选择权限">
              <el-option label="投递员" value=0></el-option>
              <el-option label="管理员" value=1></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
      <div class="manage-header">
        <el-button @click="handleAdd" type="primary">
            + 新增
        </el-button> 
        <!-- form搜索区域 -->
        <el-form :inline="true" :model="userForm">
          <el-form-item>
            <el-input placeholder="请输入名称" v-model="userForm.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
          </el-form-item>
        </el-form>
      </div> 
      <div class="common-table">      
        <el-table
        stripe
        height="90%"
        :data="tableData"
        style="width: 100%">
      <el-table-column prop="user_id" label="ID"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="password" label="密码"></el-table-column>
      <el-table-column prop="role_id" label="权限">
        <template slot-scope="scope">
          {{ formatRole(scope.row.role_id) }}
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column
          prop="edit"
          label="编辑">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
      </el-table-column>
<!-- 其他需要的列 -->

    </el-table>
      <div class="pager">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            @current-change="handlePage">
          </el-pagination>
      </div></div>
    </div>
</template>
<script>
import {getUser,addUser,editUser,delUser} from '../api'
export default {
    data(){
        return{
            user_id:'',
            dialogVisible:false,
            form: {
              username:'',
              password:'',
              roleid:'',
              email:''
            },
            rules:{
              username:[{ required: true, message: '请输入用户名'}],
              password:[{ required: true, message: '请输入密码'}],
              roleid:[{required: true, message: '选择用户权限'}],
              email:[{ required: true, message: '请输入邮箱'}]

            },
            tableData:[],
            modalType:0 ,//0表示新增的弹窗，1表示编辑的弹窗 
            total:0,//当前总条数
            pageData:{
              page:1,
              limit:10
            },
            userForm:{
              name:''
            }
              
        }
    },
    methods:{
      formatRole(roleId) {
        return roleId === 0 ? '投递员' : '管理员';
      },
      submit(){
        this.$refs.form.validate((valid)=>{
          console.log(valid,'valid')
          if(valid){
            //对表单进行处理
            if(this.modalType === 0){
                addUser(this.form).then(()=>{
                  //再次调用获取列表的接口以更新数据
                  this.getList()
                })
            }else{
                editUser(this.user_id,this.form).then(()=>{
                  //再次调用获取列表的接口以更新数据
                  this.getList()
                })
            }
            
            //表单数据
            console.log(this.form,"form")

            //重置表单
            this.$refs.form.resetFields()
            //关闭弹窗
            this.dialogVisible = false
          }
        })
      },
      handleClose(){
        //重置表单  
        console.log(this.$refs.form)
        this.$refs.form.resetFields()
        this.dialogVisible = false
      },
      cancel(){
        this.handleClose()
      },
      handleEdit(row){
        this.modalType=1
        this.dialogVisible=true
        //需要注意对当前行数据进行深拷贝，否则会出错
        this.form=JSON.parse(JSON.stringify(row))
        console.log(row.user_id)
          // 设置 user_id
        this.user_id = row.user_id; // 或者根据实际情况赋值
      },
      // handleDelete(row){
      //   this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     this.user_id = row.user_id; 
      //     console.log(this.user_id,'this.user_id')
      //     delUser(this.user_id,{id:row.id}).then(()=>{
      //       this.$message({
      //       type: 'success',
      //       message: '删除成功!'
      //     });
      //     this.getList()
      //     })
      //   }).catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消删除'
      //     });          
      //   });
      // },
      handleDelete(row) {
  this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    this.user_id = row.user_id; 
    console.log(this.user_id,'this.user_id')
    delUser(this.user_id, { id: row.id }).then(() => {
      this.$message({
        type: 'success',
        message: '删除成功!'
      });
      this.getList();
    });
  }).catch(() => {
    this.$message({
      type: 'info',
      message: '已取消删除'
    });
  });
},

      handleAdd(){
        this.modalType=0
        this.dialogVisible=true
      },
      getList(){
          //获取的列表数据
          //  getUser()
          getUser().then(({ data }) => {
    console.log(data);
    this.tableData = data;
  }).catch(error => {
    console.error("Error fetching data:", error);
  });
      },
      handlePage(val){
        //选择页面的回调函数
        // console.log(val,'val')
        this.pageData.page=val
        this.getList()
      },
      //列表的查询条件
      onSubmit(){
        this.getList()
      }
    },
    // mounted(){
    //     // this.getList()
    //     getUser().then(({ data }) => {
    //       console.log(data)
    //       this.tableData=data.list
    //     })
    // }
    mounted() {
        getUser().then(({ data }) => {
          console.log(data);
          this.tableData = data;
        }).catch(error => {
          console.error("Error fetching data:", error);
        });
}

}
</script>
<style lang="less" scoped>
.manage{
  height: 90%;
  .manage-header{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .common-table{
    position: relative;
    height: calc(100% - 62px);
    .pager{
      position:absolute;
      bottom: 0;
      right:20px;
    }
  }
}
</style>

